package com.google.code.opengw.server.initializer;

import com.google.code.opengw.support.OpenGWException;
import com.google.code.opengw.support.OpenGWLogger;

public class SystemInitializer {
	private static SystemInitializer systemInitializer;
	private boolean initialized = false;
	static {
		systemInitializer = new SystemInitializer();
	}
	
	private SystemInitializer(){
	}
	
	public static SystemInitializer getInstance(){
		return systemInitializer;
	}
	
	public synchronized void initialize() throws OpenGWException{
		if(!initialized){
			try{
				OpenGWLogger.addInfo(SystemInitializer.class, "Server initialization starts");
				OpenGWLogger.addInfo(SystemInitializer.class, "Configuration Values initialization starts");
				ConfigInitializer configInitializer = new ConfigInitializer();
				configInitializer.initialize();
				OpenGWLogger.addInfo(SystemInitializer.class, "Configuration Values initialization successfullly finished");
				
				OpenGWLogger.addInfo(SystemInitializer.class, "User initialization starts");
				UserInitializer userInitializer = new UserInitializer();
				userInitializer.initialize();
				OpenGWLogger.addInfo(SystemInitializer.class, "User initialization successfullly finished");
				
				OpenGWLogger.addInfo(SystemInitializer.class, "Certificate initialization starts");
				KeystoreInitializer keystoreInitializer = new KeystoreInitializer();
				keystoreInitializer.initialize();
				OpenGWLogger.addInfo(SystemInitializer.class, "Certificate initialization successfullly finished");
				OpenGWLogger.addInfo(SystemInitializer.class, "Server initialization successfullly finished");
				
				initialized = true;
			} catch (Exception e) {
				OpenGWLogger.addError(SystemInitializer.class, OpenGWException.INITIALIZATION_NOT_COMPLETED, e);
				throw new OpenGWException(OpenGWException.INITIALIZATION_NOT_COMPLETED, e);
			}
			
			
		} else {
			throw new OpenGWException(OpenGWException.INITIALIZATION_ALREADY_COMPLETED);
		}
		
		
	}
}
